﻿using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.SystemCore.DTO.MenuField;
using AnQinFrameWorkAPI.Entity.SystemCore.Entity;
using AnQinFrameWorkAPI.Entity.SystemCore.View.MenuField;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Entity;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.Service.SystemCore;

/// <summary>
///     菜单字段业务层
/// </summary>
public class MenuFieldService
{
    #region 获取菜单字段列表

    public async Task<RequestResponseModel> GetMenuFieldList(MenuFieldSearchDTO menuFieldSearchDTO)
    {
        //校验菜单id有效性
        var menu = await _context.Menu.FirstOrDefaultAsync(a => a.Id.ToString() == menuFieldSearchDTO.MenuId);

        if (menu == null) return RequestResponse.ErrorRequest("菜单id无效", menuFieldSearchDTO.MenuId);

        //筛选
        var query = _context.MenuField
                .ProjectTo<MenuFieldView>(_mapper.ConfigurationProvider)
                .AsNoTracking().Where(a => a.MenuId == menuFieldSearchDTO.MenuId)
                .WhereIF(
                    a => a.Key.Contains(menuFieldSearchDTO.Keyword) || a.Name.Contains(menuFieldSearchDTO.Keyword) ||
                         a.Remark.Contains(menuFieldSearchDTO.Keyword),
                    !string.IsNullOrEmpty(menuFieldSearchDTO.Keyword))
            ;

        //返回列表
        var menuField = await query
            .Page(menuFieldSearchDTO.PageIndex, menuFieldSearchDTO.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<MenuFieldView>(menuField, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 新增菜单字段

    public async Task<RequestResponseModel> AddMenuField(MenuFieldAddDTO menuFieldAddDTO)
    {
        //生成新数据
        var menuField = _mapper.Map<MenuFieldModel>(menuFieldAddDTO);

        menuField.Id = Guid.NewGuid();

        //新增数据
        _context.MenuField.Add(menuField);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(menuField.Id, "新增成功");
    }

    #endregion

    #region 更新菜单字段

    public async Task<RequestResponseModel> PutMenuField(MenuFieldUpdateDTO menuFieldUpdateDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.MenuField, menuFieldUpdateDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("菜单字段Id无效");
        ;

        //编辑数据
        var menuField = _mapper.Map(menuFieldUpdateDTO, query);

        //更新表
        _context.MenuField.Update(menuField);
        await _context.SaveChangesAsync();

        return RequestResponse.OK(menuFieldUpdateDTO.Id);
    }

    #endregion

    #region 删除菜单字段

    public async Task<RequestResponseModel> DeleteMenuField(SingleIdDTO deleteDTO)
    {
        //校验数据
        var query = await Valid.ValidId(_context.MenuField, deleteDTO.Id);
        if (query == null) return RequestResponse.ErrorRequest("菜单字段Id无效");
        ;

        //删除操作
        _context.MenuField.Remove(query);

        //保存库
        await _context.SaveChangesAsync();
        return RequestResponse.OK(deleteDTO.Id, "删除成功");
    }

    #endregion

    #region 批量删除菜单字段

    public async Task<RequestResponseModel> DeleteArrayMenuField(IdArrayDTO idArrayDTO)
    {
        //校验单据
        var query = await _context.MenuField
            .WhereIF(a => idArrayDTO.Ids.Contains(a.Id.ToString()), idArrayDTO != null)
            .ToListAsync();

        if (query.Count != idArrayDTO.Ids.Count) return RequestResponse.ErrorRequest("菜单字段Id无效");
        ;

        //删除操作
        _context.MenuField.RemoveRange(query);

        //保存库
        await _context.SaveChangesAsync();
        return RequestResponse.OK(idArrayDTO.Ids, "删除成功");
    }

    #endregion

    #region 初始化

    //数据库
    private readonly DatabaseContext _context;

    //自动映射
    private readonly IMapper _mapper;

    //构造注入
    public MenuFieldService(DatabaseContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    #endregion
}